package com.compass.doctor.ui.home.view

import android.content.Context
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.ViewArticleAndVideoBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.home.SpecialColumnActivity
import com.compass.doctor.ui.home.adapter.ArticleAndVideoAdapter
import com.compass.framework.constant.ARTICLE_DETAILS
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.constant.VIDEO_DETAILS
import com.compass.framework.ext.asActivity
import com.compass.framework.ext.onClick
import com.compass.framework.model.ArticleOrVideo

/**
 * Description： 文章视频view
 * Date:2023/12/23 22:05
 */
class ArticleAndVideoView(private var context: Context) : LinearLayout(context) {

    private var articles: MutableList<ArticleOrVideo> = ArrayList()
    private var videos: MutableList<ArticleOrVideo> = ArrayList()
    private var articleAndVideoAdapter: ArticleAndVideoAdapter//文章or视频
    private var viewArticleAndVideoBinding: ViewArticleAndVideoBinding? = null

    init {

        viewArticleAndVideoBinding =
            ViewArticleAndVideoBinding.inflate(LayoutInflater.from(context), this, true)

        articleAndVideoAdapter = ArticleAndVideoAdapter(1)

        viewArticleAndVideoBinding?.apply {
            ivMoreAction.onClick {
                context?.let { SpecialColumnActivity.start(it.asActivity,1) }

            }
            rcyArticleVideo.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = articleAndVideoAdapter
            }
            dslTab.tabIndicator.indicatorWidth = -2
            dslTab.tabIndicator.indicatorEnableFlow = true
            dslTab.configTabLayoutConfig {
                onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                    val toIndex = selectIndexList.first()
                    when (toIndex) {
                        0 -> {
                            articleAndVideoAdapter.setArticleOrVideoType(1)
                            articleAndVideoAdapter.setData(articles)
                        }

                        1 -> {
                            articleAndVideoAdapter.setArticleOrVideoType(2)
                            articleAndVideoAdapter.setData(videos)
                        }
                    }
                }
            }

            articleAndVideoAdapter.onItemClickListener = { view, position ->
                val item = articleAndVideoAdapter.getItem(position)
                item?.let {
                    //1是文章 2是视频
                    when (articleAndVideoAdapter.articleOrVideo) {
                        1 -> {
                            WebDetailActivity.start(
                                context = context,
                                url = ARTICLE_DETAILS + it.id,
                                title = it.title
                            )
                        }

                        2 -> {
                            WebDetailActivity.start(
                                context = context,
                                url = VIDEO_DETAILS + it.id,
                                title = it.title
                            )
                        }
                    }
                }
            }
        }
    }


    /**
     * 设置内容，自行扩展
     */
    fun initData(
        articlesList: MutableList<ArticleOrVideo>?,
        videosList: MutableList<ArticleOrVideo>?
    ) {
        articlesList?.let {
            this.articles = it
            articleAndVideoAdapter.setData(articles)
        }
        videosList?.let { this.videos = it }


    }
}